﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShareContent;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace AncientEmpires_025_132
{
    public class SelectMapMenu : Menu
    {
        // Event for select main menu
        public delegate void EventHandleMenuSelect();
        public event EventHandleMenuSelect MenuSelect;

        // Event for select main menu
        public delegate void EventHandleMapSelect(int iMap);
        public event EventHandleMapSelect MapSelect;

        private SelectMapMenuWithXML selectMapMenuWithXMl;
        private ListMaps listMapWithXML;
        private Sprite2D title, border, titleBorder;
        private List<MyButton> listMap;
        private MyButton btnBack;
        private MyButton cmdUp, cmdDown;
        private int currentTopMap;
        private int chosenMap;

        public int ChosenMap
        {
            get { return chosenMap; }
            set { chosenMap = value; }
        }

        public SelectMapMenu(ContentManager content, GraphicsDevice graphicsDevice, string strMenuXML, string strListMapXML)
        {

            selectMapMenuWithXMl = content.Load<SelectMapMenuWithXML>(strMenuXML);
            listMapWithXML = content.Load<ListMaps>(strListMapXML);
            SizeScale = selectMapMenuWithXMl.sizeScale;
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            currentTopMap = 0;
            chosenMap = -1;
            InitMenu(content);
        }

        public void InitMenu(ContentManager content)
        {
            Background = LoadStaticItem(content, selectMapMenuWithXMl.strBackground, selectMapMenuWithXMl.sizeScale, new Vector2(0, 0), "", new Vector2(0, 0));

            border = LoadStaticItem(content, selectMapMenuWithXMl.strBorder, selectMapMenuWithXMl.sizeScaleBorder, selectMapMenuWithXMl.positionScaleBorder, "", new Vector2(0, 0));

            title = LoadStaticItem(content, selectMapMenuWithXMl.strTitle, selectMapMenuWithXMl.sizeScaleTitle, selectMapMenuWithXMl.positionScaleTitle, "", new Vector2(0, 0));

            titleBorder = LoadStaticItem(content, selectMapMenuWithXMl.strBorderTitle, selectMapMenuWithXMl.sizeScaleBorderTitle, selectMapMenuWithXMl.positionScaleBorderTitle, "", new Vector2(0, 0));
            /*
            listMap = LoadListItem(content, selectMapMenuWithXMl.listMap, "SelectMapMenu/", new Vector2(0, 0));
            for (int i = 0; i < listMap.Count; i++)
            {
                listMap[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickMap);
                if (i > 2) listMap[i].IsHide = true;
            }*/

            listMap = new List<MyButton>();

            for (int i = 0; i < listMapWithXML.numMaxMap; i++)
            {
                if (i < listMapWithXML.listInfoStringMap.Count)
                {           
                    SliceMenu tempSlice = new SliceMenu();
                    tempSlice.sizeScale = selectMapMenuWithXMl.sizeScaleMap;

                    tempSlice.positionScale = new Vector2(selectMapMenuWithXMl.positionScaleFirstMap.X, selectMapMenuWithXMl.positionScaleFirstMap.Y + i * selectMapMenuWithXMl.mapTap);
  
                    tempSlice.strTexture = listMapWithXML.listInfoStringMap[i].strTexture;
                    MyButton tempButton = LoadItem(content,tempSlice,"",new Vector2(0,0));
                    tempButton.ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickMap);
                    listMap.Add(tempButton);
                }
                else 
                {
                    SliceMenu tempSlice = new SliceMenu();
                    tempSlice.sizeScale = selectMapMenuWithXMl.sizeScaleMap;

                    tempSlice.positionScale = new Vector2(selectMapMenuWithXMl.positionScaleFirstMap.X, selectMapMenuWithXMl.positionScaleFirstMap.Y + i * selectMapMenuWithXMl.mapTap);

                    tempSlice.strTexture = selectMapMenuWithXMl.strTextureLocked;
                    MyButton tempButton = LoadItem(content, tempSlice, "", new Vector2(0, 0));
                    listMap.Add(tempButton);
                }
                if (i > 2) listMap[i].IsHide = true;
            }

            btnBack = LoadItem(content, selectMapMenuWithXMl.btnBack, "SelectMapMenu/", new Vector2(0, 0));
            btnBack.ClickButton += new MyButton.EventHandleClick(ClickBtnBack);

            cmdUp = LoadItem(content, selectMapMenuWithXMl.cmdUp, "SelectMapMenu/", new Vector2(0, 0));
            cmdUp.ClickButton += new MyButton.EventHandleClick(ClickBtnUp);

            cmdDown = LoadItem(content, selectMapMenuWithXMl.cmdDown, "SelectMapMenu/", new Vector2(0, 0));
            cmdDown.ClickButton += new MyButton.EventHandleClick(ClickBtnDown);
        }

        public void ClickMap(object sender)
        {
            int choice = listMap.IndexOf((MyButton)sender);
            if (choice != -1 && ((MyButton)sender).IsHide == false)
            {
                chosenMap = choice;
                MapSelect(listMapWithXML.listInfoStringMap[choice].numberOfNations);
            }
        }

        public void ClickBtnBack()
        {
            MenuSelect();
        }

        public void ClickBtnUp()
        {
            if (cmdUp.IsHide == false)
            {
                for (int i = 0; i < listMap.Count; i++)
                {
                    if (i >= currentTopMap-1 && i <= currentTopMap+1) listMap[i].IsHide = false;
                    else listMap[i].IsHide = true;
                    listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y + Size.Y * selectMapMenuWithXMl.mapTap));
                }
                /*
                switch (currentTopMap)
                {
                    case 1:
                        for (int i = 0; i < listMap.Count; i++)
                        {
                            if (i >= 0 && i <= 2) listMap[i].IsHide = false;
                            else listMap[i].IsHide = true;
                            listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y + Size.Y * selectMapMenuWithXMl.mapTap));
                        }
                        break;
                    case 2:
                        for (int i = 0; i < listMap.Count; i++)
                        {
                            if (i >= 1 && i <= 3) listMap[i].IsHide = false;
                            else listMap[i].IsHide = true;
                            listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y + Size.Y * selectMapMenuWithXMl.mapTap));
                        }
                        break;
                    case 3:
                        for (int i = 0; i < listMap.Count; i++)
                        {
                            if (i >= 2 && i <= 4) listMap[i].IsHide = false;
                            else listMap[i].IsHide = true;
                            listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y + Size.Y * selectMapMenuWithXMl.mapTap));
                        }
                        break;
                }*/
                currentTopMap--;
            }
        }

        public void ClickBtnDown()
        {
            if (cmdDown.IsHide == false)
            {
                for (int i = 0; i < listMap.Count; i++)
                {
                    if (i >= currentTopMap + 1 && i <= currentTopMap +3) listMap[i].IsHide = false;
                    else listMap[i].IsHide = true;
                    listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y - Size.Y * selectMapMenuWithXMl.mapTap));
                }
                /*
                switch (currentTopMap)
                {
                    case 0:
                        for (int i = 0; i < listMap.Count; i++)
                        {
                            if (i >= 1 && i <= 3) listMap[i].IsHide = false;
                            else listMap[i].IsHide = true;
                            listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X,listMap[i].GetPosition().Y-Size.Y*selectMapMenuWithXMl.mapTap));
                        }
                        break;
                    case 1:
                        for (int i = 0; i < listMap.Count; i++)
                        {
                            if (i >= 2 && i <= 4) listMap[i].IsHide = false;
                            else listMap[i].IsHide = true;
                            listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y - Size.Y * selectMapMenuWithXMl.mapTap));
                        }
                        break;
                    case 2:
                        for (int i = 0; i < listMap.Count; i++)
                        {
                            if (i >= 3 && i <= 5) listMap[i].IsHide = false;
                            else listMap[i].IsHide = true;
                            listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y - Size.Y * selectMapMenuWithXMl.mapTap));
                        }
                        break;
                }*/
                currentTopMap++;
            }
        }

        public void UpdateMenuSize(GraphicsDevice graphicsDevice)
        {
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            Background.Size = Size;

            border.Size = new Vector2(selectMapMenuWithXMl.sizeScaleBorder.X * Size.X, selectMapMenuWithXMl.sizeScaleBorder.Y * Size.Y);
            border.Position = new Vector2(selectMapMenuWithXMl.positionScaleBorder.X * Size.X, selectMapMenuWithXMl.positionScaleBorder.Y * Size.Y);

            title.Size = new Vector2(selectMapMenuWithXMl.sizeScaleTitle.X * Size.X, selectMapMenuWithXMl.sizeScaleTitle.Y * Size.Y);
            title.Position = new Vector2(selectMapMenuWithXMl.positionScaleTitle.X * Size.X, selectMapMenuWithXMl.positionScaleTitle.Y * Size.Y);

            titleBorder.Size = new Vector2(selectMapMenuWithXMl.sizeScaleBorderTitle.X * Size.X, selectMapMenuWithXMl.sizeScaleBorderTitle.Y * Size.Y);
            titleBorder.Position = new Vector2(selectMapMenuWithXMl.positionScaleBorderTitle.X * Size.X, selectMapMenuWithXMl.positionScaleBorderTitle.Y * Size.Y);
            /*
            for (int i = 0; i < listMap.Count; i++)
            {
                listMap[i].SetPosition(new Vector2(selectMapMenuWithXMl.listMap[i].positionScale.X * Size.X, selectMapMenuWithXMl.listMap[i].positionScale.Y * Size.Y));
                listMap[i].SetSize(new Vector2(selectMapMenuWithXMl.listMap[i].sizeScale.X * Size.X, selectMapMenuWithXMl.listMap[i].sizeScale.Y * Size.Y));                
            }*/

            for (int i = 0; i < listMap.Count; i++)
            {
                listMap[i].SetPosition(new Vector2(selectMapMenuWithXMl.positionScaleFirstMap.X * Size.X, (selectMapMenuWithXMl.positionScaleFirstMap.Y + i * selectMapMenuWithXMl.mapTap) * Size.Y ));
                listMap[i].SetSize(new Vector2(selectMapMenuWithXMl.sizeScaleMap.X * Size.X, selectMapMenuWithXMl.sizeScaleMap.Y * Size.Y));
            }

            switch (currentTopMap)
            {
                case 0:
                    for (int i = 0; i < listMap.Count; i++)
                    {
                        listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y));
                    }
                    break;
                case 1:
                    for (int i = 0; i < listMap.Count; i++)
                    {
                        listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y - Size.Y * selectMapMenuWithXMl.mapTap*currentTopMap));
                    }
                    break;
                case 2:
                    for (int i = 0; i < listMap.Count; i++)
                    {
                        listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y - Size.Y * selectMapMenuWithXMl.mapTap*currentTopMap));
                    }
                    break;
                case 3:
                    for (int i = 0; i < listMap.Count; i++)
                    {
                        listMap[i].SetPosition(new Vector2(listMap[i].GetPosition().X, listMap[i].GetPosition().Y - Size.Y * selectMapMenuWithXMl.mapTap*currentTopMap));
                    }
                    break;
            }

            btnBack.SetPosition(new Vector2(selectMapMenuWithXMl.btnBack.positionScale.X * Size.X, selectMapMenuWithXMl.btnBack.positionScale.Y * Size.Y));
            btnBack.SetSize(new Vector2(selectMapMenuWithXMl.btnBack.sizeScale.X * Size.X, selectMapMenuWithXMl.btnBack.sizeScale.Y * Size.Y));

            cmdUp.SetPosition(new Vector2(selectMapMenuWithXMl.cmdUp.positionScale.X * Size.X, selectMapMenuWithXMl.cmdUp.positionScale.Y * Size.Y));
            cmdUp.SetSize(new Vector2(selectMapMenuWithXMl.cmdUp.sizeScale.X * Size.X, selectMapMenuWithXMl.cmdUp.sizeScale.Y * Size.Y));

            cmdDown.SetPosition(new Vector2(selectMapMenuWithXMl.cmdDown.positionScale.X * Size.X, selectMapMenuWithXMl.cmdDown.positionScale.Y * Size.Y));
            cmdDown.SetSize(new Vector2(selectMapMenuWithXMl.cmdDown.sizeScale.X * Size.X, selectMapMenuWithXMl.cmdDown.sizeScale.Y * Size.Y));
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Background.Draw(gameTime, spriteBatch);

            border.Draw(gameTime, spriteBatch);

            title.Draw(gameTime, spriteBatch);

            titleBorder.Draw(gameTime, spriteBatch);
            
            for (int i = 0; i < listMapWithXML.numMaxMap; i++)
            {
                listMap[i].Draw(gameTime, spriteBatch);
            }

            btnBack.Draw(gameTime, spriteBatch);

            cmdUp.Draw(gameTime, spriteBatch);

            cmdDown.Draw(gameTime, spriteBatch);
        }

        public override void Update(GameTime gametime)
        {
            Background.Update(gametime);

            border.Update(gametime);

            title.Update(gametime);

            titleBorder.Update(gametime);

            for (int i = 0; i < listMapWithXML.numMaxMap; i++)
            {
                listMap[i].Update(gametime);
            }

            btnBack.Update(gametime);

            if (currentTopMap <= 0) cmdUp.IsHide = true;
            else cmdUp.IsHide = false;
            cmdUp.Update(gametime);

            if (currentTopMap >= listMap.Count-3) cmdDown.IsHide = true;
            else cmdDown.IsHide = false;
            cmdDown.Update(gametime);
        }
    }
}
